/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mxi.its.dds2.Bean;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import mx.its.dsw2.biblioteca.Categoria;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.ToggleEvent;
import org.primefaces.event.UnselectEvent;

/**
 *
 * @author Carlos Asis
 */
@ManagedBean
@SessionScoped
public class CategoryBean {

    @EJB
    private CategoriaBlLocal categoryBl;
    private Categoria category;
    private List<Categoria> list;
    private List<Categoria> filteredUser;

    public Categoria getCategory() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
    }

    public List<Categoria> getList() {
        list = categoryBl.getListAll();
        return list;
    }

    public void setList(List<Categoria> list) {
        this.list = list;
    }

    public List<Categoria> getFilteredUser() {
        return filteredUser;
    }

    public void setFilteredUser(List<Categoria> filteredUser) {
        this.filteredUser = filteredUser;
    }

    public String prepararCategoria() {
        category = new Categoria();
        return "CategoriaCrearEditar";
    }

    /////////////////////Para el registro category saber cuando se abre//////////////
    public void handleToggle(ToggleEvent event) {

        if (event.getVisibility().name().equals("VISIBLE")) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, event.getComponent().getId() + " toggled", "Status:" + event.getVisibility().name());
            addMessage(message);
            // this.reset();
            category = new Categoria();
        } else if (event.getVisibility().name().equals("HIDDEN")) {
            this.reset();
            category = new Categoria();
        }
    }

    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);


    }

    public void reset() {
        RequestContext.getCurrentInstance().reset("form:pnl");
    }

    public String crearCategoria() {
        categoryBl.register(category);

        return "CategoryFacelets";
    }
    /////////////termina 
    //////////////Editar datos de la tabla///////////////

    public void Edit(SelectEvent event) {
        FacesMessage msg = new FacesMessage("Category Selected", category.getClave());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void RowUnselect(UnselectEvent event) {
        FacesMessage msg = new FacesMessage("Category Unselected", ((Categoria) event.getObject()).getClave());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public String editCategory(){
        categoryBl.modify(category);
        return "CategoryFacelets";
    }

    ////////////////////////terminba edicion
    /**
     * Creates a new instance of CategoriaBean
     */
    /**
     * Creates a new instance of CategoryBean
     */
    public CategoryBean() {
        list = new ArrayList<Categoria>();
        category=new Categoria();
    }
}
